<?php

namespace app\api\controller;

use app\admin\logic\article\ArticleCategoryLogic;
use app\admin\logic\article\ArticleLogic;
use app\basic\ApiController;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("文档管理")
 */
class FileController extends ApiController
{
    protected $noNeedLogin = ['categoryList','articleList','articleNewList','categoryRecommendedList','categoryOneList','detail'];

    public function __construct()
    {
        $this->logic = new ArticleCategoryLogic();
        parent::__construct();
    }


    /**
     * @Apidoc\Title("分类列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("category_id", type="int", default="0", desc="分类id")
     */
    public function categoryList()
    {
       $category_id = $this->request->post('category_id');
       $where = [];
       $where['status'] = 1;
       $where['parent_id'] = 0;
       if($category_id){
           $where['parent_id'] = $category_id;
       }
        $list = $this->logic->where($where)->order("sort","desc")->field("id,category_name")->select();
        return $this->success('success', $list);
    }


    /**
     * @Apidoc\Title("首页获取推荐的分类和文章列表")
     * @Apidoc\Method("POST")
     */
    public function categoryRecommendedList()
    {
       $category_id = $this->request->post('category_id');
       $where = [];
       $where['status'] = 1;
       $where['parent_id'] = 0;
       $where['is_index'] = 1;
       if($category_id){
           $where['parent_id'] = $category_id;
       }
       $category = $this->logic->where($where)->order("sort","desc")->field("id,category_name")->select();
         // 获取分类下的文章
        $articleLogic = new ArticleLogic();
        foreach ($category as $key=>$value) {
             // 获取当前一级分类下面的子分类
            $subCategory = $this->logic->where(['parent_id'=>$value['id']])->column('id');
            $category[$key]['articles'] = $articleLogic->getRecommendedArticles($subCategory)['list'];
        }
       return $this->success('success', $category);
    }

    /**
     * @Apidoc\Title("根据一级分类获取所有文章")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("category_id", type="int", default="0", desc="分类id")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     */
    public function categoryOneList()
    {
       $category_id = $this->request->post('category_id');
       $page = $this->request->post('page', 1);
       $limit = $this->request->post('limit', 10);
       $where = [];
       $where['status'] = 1;
       $where['parent_id'] = $category_id;
       $subCategory = $this->logic->where($where)->order("sort","desc")->select();
       $subCategory_id = array_column($subCategory->toArray(),'id');
       $articleLogic = new ArticleLogic();
       $articlesList = $articleLogic->getRecommendedArticles($subCategory_id,$limit,$page);
       return $this->success('success', $articlesList);
    }

    /**
     * @Apidoc\Title("根据二级分类获取文档列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("category_id", type="int", default="0", desc="分类id")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     * @Apidoc\Param("keyword", type="string", default="", desc="搜索关键词")
     * @Apidoc\Returned("data", type="array", desc="文档列表", children={
     * @Apidoc\Returned("id", type="int", desc="文档ID"),
     * @Apidoc\Returned("title", type="string", desc="文档标题"),
     * @Apidoc\Returned("description", type="string", desc="文档描述"),
     * @Apidoc\Returned("content", type="string", desc="文档内容"),
     * @Apidoc\Returned("create_time", type="string", desc="创建时间"),
     * @Apidoc\Returned("update_time", type="string", desc="更新时间"),
     * @Apidoc\Returned("status", type="int", desc="状态:0=禁用,1=启用"),
     * @Apidoc\Returned("category_id", type="int", desc="分类ID")
     * })
     */
    public function articleList()
    {
        // 获取请求参数
        $categoryId = $this->request->post('sub_category_id', 0);
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 10);
        $keyword = $this->request->post('keyword', '');
        // 调用逻辑层方法获取数据
        $articleLogic = new ArticleLogic();
        $data = $articleLogic->getArticleList($categoryId, $page, $limit, $keyword);
        return $this->success($data);
    }

     /**
     * @Apidoc\Title("首页获取最新的单篇")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("page", type="int", default="1", desc="页码")
     * @Apidoc\Param("limit", type="int", default="10", desc="每页数量")
     * @Apidoc\Returned("data", type="array", desc="文档列表", children={
     * @Apidoc\Returned("id", type="int", desc="文档ID"),
     * @Apidoc\Returned("title", type="string", desc="文档标题"),
     * @Apidoc\Returned("description", type="string", desc="文档描述"),
     * @Apidoc\Returned("content", type="string", desc="文档内容"),
     * @Apidoc\Returned("create_time", type="string", desc="创建时间"),
     * @Apidoc\Returned("update_time", type="string", desc="更新时间"),
     * @Apidoc\Returned("status", type="int", desc="状态:0=禁用,1=启用"),
     * @Apidoc\Returned("category_id", type="int", desc="分类ID")
     * })
     */
    public function articleNewList()
    {
        // 获取请求参数
        $page = $this->request->post('page', 1);
        $limit = $this->request->post('limit', 10);
        // 调用逻辑层方法获取数据
        $articleLogic = new ArticleLogic();
        $data = $articleLogic->getArticleList(0,$page, $limit,'','');
        return $this->success($data);
    }

    /**
     * @Apidoc\Title("获取文档详情")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", require=true, desc="文档ID")
     * @Apidoc\Returned("data", type="object", desc="文档详情", children={
     *     @Apidoc\Returned("id", type="int", desc="文档ID"),
     *     @Apidoc\Returned("title", type="string", desc="文档标题"),
     *     @Apidoc\Returned("description", type="string", desc="文档描述"),
     *     @Apidoc\Returned("content", type="string", desc="文档内容"),
     *     @Apidoc\Returned("create_time", type="string", desc="创建时间"),
     *     @Apidoc\Returned("update_time", type="string", desc="更新时间"),
     *     @Apidoc\Returned("category_id", type="int", desc="分类ID"),
     *     @Apidoc\Returned("category_name", type="string", desc="分类名称")
     * })
     */
    public function detail()
    {
        $id = $this->request->post('id', 0);
        if (empty($id)) {
            return $this->fail('参数错误');
        }
          // 获取当前用户当前文档是否收藏
        $userId = $this->auth->getUserId() ?? 0;
        $articleLogic = new \app\api\logic\ArticleLogic();
        $detail = $articleLogic->getArticleDetail($id,$userId);
        $isCollect = $articleLogic->isCollect($userId, $id);
        $detail['is_collect'] = $isCollect;
         // 获取当前用户是否已购买此文档
        $userDocLogic = new \app\api\logic\UserDocLogic();
        $detail['is_buy'] = $userDocLogic->checkUserBought($userId, $id) ? 1 : 0;
        if($detail['is_buy'] == 0) {
             //因为有些字段目前不能返回前端 需要删除
            unset($detail['filepath'],$detail['oss_link'],$detail['oss_pwd']);
        }
        if (empty($detail)) {
            return $this->fail('文档不存在');
        }
        return $this->success($detail);
    }

}